/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2015年3月20日         Root.Lu
 */

package com.coocaa.app.gamecenter.pages.detail;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;

import com.coocaa.x.framework.utils.UIUtils;
import com.coocaa.x.uipackage.BaseView;
import com.coocaa.x.uipackage.utils.FrescoUtils;
import com.coocaa.x.uipackage.widget.CoocaaDraweeView;
import com.coocaa.x.uipackage.widget.SlideFocusView;
import com.tianci.appstore.R;

import java.util.List;

public class ADView extends BaseView implements SlideFocusView.ISlideFocusView {
    public static class ADItemData {
        public String adImageUrl;
        public Intent action;

        public ADItemData(String adImageUrl, Intent action) {
            this.adImageUrl = adImageUrl;
            this.action = action;
        }
    }

    private static class ADItemiew extends CoocaaDraweeView implements OnClickListener {
        private Intent action;
        private Context mContext;

        public ADItemiew(Context context, Intent action) {
            super(context);
            mContext = context;
            setOnClickListener(this);
            this.action = action;
            getHierarchy().setPlaceholderImage(R.drawable.game_center_default);
        }

        @Override
        public void onClick(View v) {
            try {
                mContext.startActivity(action);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private LinearLayout contentView;
    private HorizontalScrollView scrollView;
    private boolean isDestroyed = false;

    public ADView(Context context) {
        super(context);
        setFocusable(false);
        setFocusableInTouchMode(false);
        scrollView = new HorizontalScrollView(context);
        scrollView.setFocusable(false);
        scrollView.setFocusableInTouchMode(false);
        scrollView.setHorizontalScrollBarEnabled(false);
        scrollView.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT));
        addView(scrollView);

        contentView = new LinearLayout(context);
        contentView.setFocusable(false);
        contentView.setFocusableInTouchMode(false);
        contentView.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT));
        contentView.setOrientation(LinearLayout.HORIZONTAL);

        scrollView.addView(contentView);
    }

    public void update(List<ADItemData> datas) {
        for (ADItemData data : datas) {
            final int index = datas.indexOf(data);
            final ADItemiew view = new ADItemiew(mContext, data.action);
            contentView.addView(view);
            UIUtils.setViewSize(view, Div(450), Div(200));
            view.startAnimation(AnimationUtils.loadAnimation(mContext, R.anim.grid_anim));
            FrescoUtils.resize(view, Uri.parse(data.adImageUrl), Div(450), Div(200), null);
            if (index > 0)
                UIUtils.setViewMargin(view, Div(32), -1, -1, -1);
//            imageCache.load(data.adImageUrl, new LiteImageCacheLoadListener() {
//                @Override
//                public void onImageLoaded(final LiteImageCacheObject obj) {
//                    synchronized (imageCacheObjects) {
//                        if (isDestroyed)
//                            obj.recyle();
//                        else {
//                            imageCacheObjects.add(obj);
//                            runOnUiThread(new Runnable() {
//                                @Override
//                                public void run() {
//                                    try {
//                                        view.setImageBitmap(obj.getBitmap());
//                                    } catch (Exception e) {
//                                        // TODO Auto-generated catch block
//                                        e.printStackTrace();
//                                    }
//                                    if (index > 0)
//                                        UIUtils.setViewMargin(view, Div(32), -1, -1, -1);
//                                }
//                            });
//                        }
//                    }
//                }
//            }, Div(450), Div(200));
        }
    }

    public void destroy() {
//        synchronized (imageCacheObjects) {
//            isDestroyed = true;
//            for (LiteImageCacheObject object : imageCacheObjects)
//                try {
//                    object.recyle();
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            imageCacheObjects.clear();
//        }
    }

    @Override
    public void initSlideFocusView(SlideFocusView parent) {
        int child_count = contentView.getChildCount();
        for (int i = 0; i < child_count; i++) {
            View view = contentView.getChildAt(i);
            view.setFocusable(true);
            view.setFocusableInTouchMode(true);
            // view.setFocusable(true);
            // view.setFocusableInTouchMode(true);
            parent.focusChangedEvent.registerView(view, new SlideFocusView.FocusViewRevision(Div(36), Div(80)));
        }
    }
}
